package com.drinks.yiniang.adapter.banner;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.community.CommunityNewsJson;
import com.drinks.yiniang.ui.activity.goods.GoodsDetailsActivity;
import com.drinks.yiniang.ui.widget.layout.RandomTagLayout;
import com.drinks.yiniang.ui.widget.view.RandomDragTagView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.adapter.BaseRecyclerBannerAdapter;
import com.hansen.library.listener.CheckDoubleClickListener;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.utils.ArithmeticUtil;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author han
 * @date 2019/12/17
 */
public class CommunityBannerRecyclerAdapter extends BaseRecyclerBannerAdapter<CommunityBannerRecyclerAdapter.ViewHolder> {

    private Context mContext;

    private RandomDragTagView mTagView;

    private List<CommunityNewsJson.CommunityNewsImg> mBannerData;

    private StringBuilder mBuilder;

    private int mCurrentWidth;
    private float width, height, x, y;

    public CommunityBannerRecyclerAdapter(Context context) {
        super(context);
        this.mContext = context;
        this.mBannerData = new ArrayList<>();

        mCurrentWidth = ScreenSizeUtils.getWidth(mContext);
    }

    @Override
    public int getItemCountReal() {
        return CommonUtils.getListSize(mBannerData);
    }

    @Override
    public int getItemCount() {
        // 如果设置为 Integer.MAX_VALUE 则会和 ViewPager 产生左右滑动冲突，具体原因未知
        // 1. 设置为 3000000(这个数值可自行测试得出) ，不用自定义 RecyclerView ；
        // 2. 自定义 RecyclerView 重写 dispatchTouchEvent 方法， 调用 getParent().requestDisallowInterceptTouchEvent(true) ，这样就可以设置为 Integer.MAX_VALUE 。
        // 详情访问：https://juejin.im/post/5d0c7693f265da1b6a349c5f
        return getItemCountReal();
    }

    @Override
    public CommunityNewsJson.CommunityNewsImg getItem(int position) {
        return mBannerData.get(position % getItemCountReal());
    }

    @Override
    public int getViewResourceId(int viewType) {
        return R.layout.item_community_list_banner;
    }

    @Override
    public ViewHolder getViewHolder(View view, int viewType) {
        return new ViewHolder(view);
    }

    @Override
    public void setView(final ViewHolder holder, final int position) {
        GlideUtils.loadBannerImage(mContext, holder.iv_community_list_banner, getItem(position % getItemCountReal()).getOriginal());

        addGoodsTag(holder.fl_community_list_tag, getItem(position % getItemCountReal()).getGoods());
    }

    public List<CommunityNewsJson.CommunityNewsImg> getBannerData() {
        return mBannerData;
    }

    public void addBannerData(List<CommunityNewsJson.CommunityNewsImg> bannerData) {
        if (mBannerData == null) {
            mBannerData = new ArrayList<>();
        }

        mBannerData.clear();

        if (bannerData != null) {
            this.mBannerData.addAll(bannerData);
        }
    }

    //添加标签tag
    private void addGoodsTag(RandomTagLayout tagLayout, List<CommunityNewsJson.CommunityNewsGoodsTag> goodsTagList) {
        tagLayout.removeAllViews();

        if (CommonUtils.isEmptyList(goodsTagList)) {
            return;
        }

        for (final CommunityNewsJson.CommunityNewsGoodsTag tagData : goodsTagList) {
            mBuilder = new StringBuilder(StringUtils.getNoNullString(tagData.getGoods_name()));
            mBuilder.append(" ¥");
            mBuilder.append(StringUtils.getNoNullString(tagData.getApp_price()));

            width = StringUtils.getFloat(tagData.getWidth());
            height = StringUtils.getFloat(tagData.getHeight());

            x = StringUtils.getFloat(tagData.getX());
            y = StringUtils.getFloat(tagData.getY());

            if (width > 0 && height > 0) {
                x =  ArithmeticUtil.divFloat2(ArithmeticUtil.mulScaleFloat2(mCurrentWidth, x), width);
                y = ArithmeticUtil.divFloat2(ArithmeticUtil.mulScaleFloat2(mCurrentWidth, y), height);

                mTagView = tagLayout.addTagView(mBuilder.toString(), x, y);

                if (mTagView != null) {
                    mTagView.setOnClickListener(new CheckDoubleClickListener() {
                        @Override
                        public void onCheckDoubleClick(View v) {
                            Intent intent = new Intent(mContext, GoodsDetailsActivity.class);
                            intent.putExtra(Constants.KeyGoodsId, tagData.getGoods_id());
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    class ViewHolder extends BaseRecyclerBannerAdapter.ViewHolder {

        RatioRoundImageView iv_community_list_banner;

        RandomTagLayout fl_community_list_tag;

        ViewHolder(View itemView) {
            super(itemView);

            iv_community_list_banner = itemView.findViewById(R.id.iv_community_list_banner);

            fl_community_list_tag = itemView.findViewById(R.id.fl_community_list_tag);
        }
    }
}